package com.huige.controller;

import com.huige.anno.Log;
import com.huige.pojo.Emp;
import com.huige.pojo.PageBean;
import com.huige.pojo.Result;
import com.huige.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * ClassName: EmpController
 * Package: com.huige.controller
 * Description:
 *
 * @Author 朱志辉
 * @Create 2024/4/19 14:40
 * @Version 1.0
 */

/*
 * 员工管理Controller
 * */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 员工分页查询
     * @param page
     * @param pageSize
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @return
     */
    @GetMapping()
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
//        if (page==null) page = 1;
//        if (pageSize==null) pageSize = 10;
        log.info("分页查询,参数:{},{},{},{},{}.{}", page, pageSize, name, gender, begin, end);
        //调用service分页查询
        PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
        return Result.success(pageBean);
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除操作,ids:{}", ids);
        //调用service的批量删除
        empService.delete(ids);
        return Result.success();

    }


    /**
     * 添加员工
     * @param emp
     * @return
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Emp emp) {
        log.info("添加员工,{}",emp);
        //调用service的添加员工
        empService.add(emp);
        return Result.success();
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result GetId(@PathVariable Integer id) {
        log.info("根据查询员工信息,id:{}",id);
        //调用service中的id查询
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }


    /**
     * 修改员工信息
     * @return
     */
    @Log
    @PutMapping
    public Result revise(@RequestBody Emp emp) {
        log.info("修改员工信息:{}",emp);
        //调用service中的修改方法
        empService.upDate(emp);
        return Result.success();
    }
}





